package com.rr.mymoney.init;

import java.util.List;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.rr.mymoney.bo.CategoryBo;
import com.rr.mymoney.bo.CurrencyBo;
import com.rr.mymoney.bo.UserBo;
import com.rr.mymoney.model.Category;
import com.rr.mymoney.model.Currency;
import com.rr.mymoney.model.User;

@Component
public class ApplicationSetup implements InitializingBean {
    private UserBo userBo;
    private CategoryBo categoryBo;
    private CurrencyBo currencyBo;

    @Autowired
    private ApplicationSetup(UserBo userBo, CategoryBo categoryBo, CurrencyBo currencyBo) {
	this.userBo = userBo;
	this.categoryBo = categoryBo;
	this.currencyBo = currencyBo;
    }

    public void afterPropertiesSet() throws Exception {
	// TODO Auto-generated method stub

	List<Currency> currencies = currencyBo.getAll();
	if (CollectionUtils.isEmpty(currencies)) {
	    Currency currency = new Currency();
	    currency.setId("RON");
	    currency.setLabel("RON");
	    currencyBo.saveOrUpdate(currency);

	    currency = new Currency();
	    currency.setId("EUR");
	    currency.setLabel("EUR");
	    currencyBo.saveOrUpdate(currency);

	    currency = new Currency();
	    currency.setId("USD");
	    currency.setLabel("USD");
	    currencyBo.saveOrUpdate(currency);
	}

	List<Category> genericCategories = categoryBo.getByUserId(null, false);
	if (CollectionUtils.isEmpty(genericCategories)) {
	    Category foodCategory = new Category();
	    foodCategory.setName("Food");
	    foodCategory.setDescription("Food costs");
	    categoryBo.saveOrUpdate(foodCategory);

	    Category carCategory = new Category();
	    carCategory.setName("Car");
	    carCategory.setDescription("Car costs");
	    categoryBo.saveOrUpdate(carCategory);

	    Category fuelCategory = new Category();
	    fuelCategory.setName("Fuel");
	    fuelCategory.setDescription("Fuel costs");
	    fuelCategory.setParentCategory(carCategory);
	    categoryBo.saveOrUpdate(fuelCategory);

	    Category insuranceCategory = new Category();
	    insuranceCategory.setName("Insurance");
	    insuranceCategory.setDescription("Insurance costs");
	    insuranceCategory.setParentCategory(carCategory);
	    categoryBo.saveOrUpdate(insuranceCategory);

	    Category houseCategory = new Category();
	    houseCategory.setName("House");
	    houseCategory.setDescription("House costs");
	    categoryBo.saveOrUpdate(houseCategory);

	    Category rentCategory = new Category();
	    rentCategory.setName("Rent");
	    rentCategory.setDescription("Rent costs");
	    rentCategory.setParentCategory(houseCategory);
	    categoryBo.saveOrUpdate(rentCategory);

	    Category maintenanceCategory = new Category();
	    maintenanceCategory.setName("Maintenance");
	    maintenanceCategory.setDescription("Maintenance costs");
	    maintenanceCategory.setParentCategory(houseCategory);
	    categoryBo.saveOrUpdate(maintenanceCategory);

	    Category otherCategory = new Category();
	    otherCategory.setName("Others");
	    otherCategory.setDescription("Other costs");
	    categoryBo.saveOrUpdate(otherCategory);
	}

	User admin = userBo.get("admin");
	if (admin == null) {
	    admin = new User();
	    admin.setUsername("admin");
	    admin.setPassword("admin");
	    userBo.saveOrUpdate(admin);
	}
    }
}
